package com.chzc.fm.duotin.ui.mine;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.adapter.recyclerview.DownloadRecyclerAdapter;
import com.chzc.fm.duotin.bean.mine.FileInfo;
import com.chzc.fm.duotin.event.DownloadEvent;
import com.chzc.fm.duotin.event.LoginEvent;
import com.chzc.fm.duotin.ui.mine.service.DownloadService;
import com.chzc.fm.duotin.ui.mine.utils.DownloadTask;
import com.chzc.fm.duotin.ui.mine.utils.MineUrl;
import com.chzc.fm.duotin.ui.other.BaseActivity;
import com.chzc.fm.duotin.utils.LogUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

public class DownloadActivity extends BaseActivity implements View.OnClickListener{

    private TextView textTitle;
    private Button btnDown, btnStop;
    private ProgressBar progressBar;
    private FileInfo fileInfo;

    private List<FileInfo> fileInfoList = null;
    private RecyclerView recyclerView;

    private DownloadRecyclerAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_download;
    }

    @Override
    protected void initViews() {
        EventBus.getDefault().register(this);
        textTitle = (TextView) findViewById(R.id.tv_download_show);
        btnDown = (Button) findViewById(R.id.btn_download_download);
        btnStop = (Button) findViewById(R.id.btn_download_stop);
        progressBar = (ProgressBar) findViewById(R.id.pb_download_show);

        recyclerView = (RecyclerView) findViewById(R.id.rv_down_content);
    }

    @Override
    protected void initEvents() {
        btnDown.setOnClickListener(this);
        btnStop.setOnClickListener(this);
    }

    @Override
    protected void init() {
        //创建文件信息对象
        fileInfo = new FileInfo(0, MineUrl.DOWNLOAD_URL, "Music", 0, 0);
        progressBar.setMax(100);

        //注册广播接收器
        IntentFilter filter = new IntentFilter();
        filter.addAction(DownloadService.ACTION_UPDATE);
        registerReceiver(mReceiver, filter);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        fileInfoList = new ArrayList<>();
        adapter = new DownloadRecyclerAdapter(this, fileInfoList);
        recyclerView.setAdapter(adapter);

    }

    @Override
    public void onClick(View v) {
        Intent i;
        switch (v.getId()){
            case R.id.btn_download_download:
                i = new Intent(this, DownloadService.class);
                i.setAction(DownloadService.ACTION_START);
                i.setPackage(getPackageName());
                i.putExtra("fileInfo", fileInfo);
                startService(i);
                break;
            case R.id.btn_download_stop:
                i = new Intent(this, DownloadService.class);
                i.setAction(DownloadService.ACTION_STOP);
                i.setPackage(getPackageName());
                i.putExtra("fileInfo", fileInfo);
                startService(i);
                break;
        }
    }

    BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(DownloadService.ACTION_UPDATE.equals(intent.getAction())){
                int finished = intent.getIntExtra("finished", 0);
                progressBar.setProgress(finished);
                LogUtil.w("" + new StringBuffer().append(finished).append("%"));
            }
        }
    };

    /**
     * eventBus处理登录回调的函数
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(DownloadEvent event){
        switch (event.getState()){
            case DownloadEvent.NONE:
                break;
            case DownloadEvent.PAUSE:
                break;
            case DownloadEvent.PREPARE:
                break;
            case DownloadEvent.UPDATE:
                break;
            case DownloadEvent.UPDATE_FILE:
                updateFile(event);
                break;
        }
    }

    private void updateFile(DownloadEvent event) {
        Queue<FileInfo> fileInfoQueue;
        fileInfoQueue = event.getFileInfoQueue();
        fileInfoList.clear();
        for(int i = 0; i<fileInfoQueue.size(); i++){
            fileInfoList.add(fileInfoQueue.poll());
        }
        adapter.setFileInfoList(fileInfoList);


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

        if(mReceiver != null){
            unregisterReceiver(mReceiver);
        }

        EventBus.getDefault().unregister(this);
    }
}
